<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @row-save="rowSave"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          type="text"
          @click.stop="handleView(scope.row, scope.index)"
          >详情</el-button
        >
      </template>
      <template slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click.stop="handleSave"
          >申请开通分账业务</el-button
        >
      </template>
      <template slot-scope="{}" slot="serviceNoSearch">
        <select-page
          ref="searchSerSelect"
          label="serviceName"
          value="serviceNo"
          :select.sync="search.serviceNo"
          :url="website.searchLink.serviceUrl"
          placeholder-text="服务商"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page
          ref="searchRpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.rpiNo"
          :url="website.searchLink.merchantUrl"
          placeholder-text="商户"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="projectCodeSearch">
        <select-page
          ref="searchProjectSelect"
          label="projectName"
          value="projectCode"
          :select.sync="search.projectCode"
          :url="'division/division_project/pageList'"
          placeholder-text="项目"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="aisleCodeSearch">
        <select-page
          ref="searchAisSelect"
          label="aisleName"
          value="aisleCode"
          :select.sync="search.aisleCode"
          :url="'aisle/rbp_pay_aisle/page'"
          placeholder-text="通道"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{ row }" slot="aisleCode">
        <span>{{ row.aisleName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionKind">
        <span>{{ row.divisionKindName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionScope">
        <span>{{ row.divisionScopeName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionType">
        <span>{{ row.divisionTypeName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionMode">
        <span>{{ row.divisionModeName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionSettleType">
        <span>{{ row.divisionSettleTypeName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionScope">
        <span>{{ row.divisionScopeName }}</span>
      </template>
      <template slot-scope="{ row }" slot="divisionRule">
        <span>{{ row.divisionRuleName }}</span>
      </template>
    </avue-crud>

    <!-- 表单弹窗 -->
    <el-dialog
      class="el-dialog__wrapper avue-dialog avue-crud__dialog"
      :title="dialogText"
      :visible.sync="dialogVisible"
      append-to-body
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :model="merchantForm"
        :rules="rules"
        label-width="120px"
      >
        <div class="haryuki-form-title haryuki-m-b-10 haryuki-m-t-20">
          分账业务
        </div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectCode">
              <select-page
                ref="searchProjectSelect"
                label="projectName"
                value="projectCode"
                :select.sync="merchantForm.projectCode"
                :url="'division/division_project/pageList'"
                placeholder-text="项目"
                filter-key="searchParam"
                @change="projectChange"
              ></select-page>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商户名称" prop="rpiNo">
              <select-page
                ref="searchRpiSelect"
                label="rpiName"
                value="rpiNo"
                :select.sync="merchantForm.rpiNo"
                :url="website.searchLink.merchantUrl"
                placeholder-text="商户"
                filter-key="searchParam"
                :params="rpiParams"
              ></select-page>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="haryuki-form-title haryuki-m-b-10 haryuki-m-t-20">
          分账信息
        </div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="分账种类" prop="divisionKind">
              <select-dict
                ref="divisionKind"
                :select.sync="merchantForm.divisionKind"
                code="division_kind"
                placeholder-text="分账种类"
              ></select-dict>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分账范围" prop="divisionScope">
              <select-dict
                ref="divisionScope"
                :select.sync="merchantForm.divisionScope"
                code="division_scope"
                placeholder-text="分账范围"
              ></select-dict>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分账类型" prop="divisionType">
              <select-dict
                ref="divisionType"
                :select.sync="merchantForm.divisionType"
                code="division_type"
                placeholder-text="分账类型"
              ></select-dict>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分账方式" prop="divisionMode">
              <select-dict
                ref="divisionMode"
                :select.sync="merchantForm.divisionMode"
                code="division_mode"
                placeholder-text="分账类型"
              ></select-dict>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算类型" prop="settleType">
              <select-dict
                ref="divisionSettleType"
                :select.sync="merchantForm.settleType"
                code="division_settle_type"
                placeholder-text="分账类型"
              ></select-dict>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分账规则" prop="divisionRule">
              <select-dict
                ref="divisionRule"
                :select.sync="merchantForm.divisionRule"
                code="division_rule"
                placeholder-text="分账类型"
              ></select-dict>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span
        slot="footer"
        class="avue-dialog__footer avue-dialog__footer--right"
      >
        <el-button size="small" type="primary" v-preventClick @click="handleSubmit"
          >确 定</el-button
        >
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList,
  add,
} from "@/api/rb-oem/separateMerchant/separate_merchant";
import { mapGetters } from "vuex";
import selectPage from "@/components/select-page/select-page";
import selectDict from "@/components/select-dict/select-dict";

export default {
  name: "SeparateMerchant",
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(100vh - 200px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 120,
        searchLabelWidth: 120,
        // menuWidth: 150,
        emptyBtnText: "重置",
        column: [
          {
            label: "项目编号",
            searchLabel: "项目名称",
            prop: "projectCode",
            type: "select",
            search: true,
            searchOrder: 4,
            overHidden: true,
            width: 140,
            rules: [
              {
                required: true,
                message: "请选择服务商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "项目名称",
            prop: "projectName",
            type: "input",
            width: 180,
          },
          {
            label: "服务商编号",
            searchLabel: "服务商",
            prop: "serviceNo",
            type: "select",
            search: true,
            searchOrder: 1,
            overHidden: true,
            width: 140,
            rules: [
              {
                required: true,
                message: "请选择服务商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入服务商名称",
                trigger: "blur",
              },
            ],
            overHidden: true,
            width: 180,
          },
          {
            label: "",
            prop: "isSelectChild",
            type: "select",
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            search: true,
            searchType: "checkbox",
            searchLabelWidth: "0",
            searchOrder: 2,
            dicData: [
              {
                label: "包含下级",
                value: 1,
              },
            ],
          },
          {
            label: "商户编号",
            searchLabel: "商户",
            prop: "rpiNo",
            type: "select",
            width: 140,
            display: false,
            search: true,
            searchOrder: 3,
            rules: [
              {
                required: true,
                message: "请输入商户号",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商户名称",
            prop: "rpiName",
            type: "input",
            width: 180,
          },
          {
            label: "分账通道",
            prop: "aisleCode",
            type: "select",
            props: {
              label: "aisleName",
              value: "aisleCode",
            },
            display: false,
            search: true,
            clearable: true,
            rules: [
              {
                required: true,
                message: "请选择通道编号",
                trigger: "blur",
              },
            ],
            overHidden: true,
            width: 140,
          },
          {
            label: "分账种类",
            prop: "divisionKind",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=division_kind",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            showColumn: false,
            width: 110,
          },
          {
            label: "分账范围",
            prop: "divisionScope",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=division_scope",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            showColumn: false,
            width: 110,
          },
          {
            label: "分账类型",
            prop: "divisionType",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=division_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            showColumn: false,
            width: 110,
          },
          {
            label: "分账方式",
            prop: "divisionMode",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=division_mode",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            showColumn: false,
            width: 110,
          },
          {
            label: "结算类型",
            prop: "divisionSettleType",
            type: "select",
            search: true,
            dicUrl:
              "/api/blade-system/dict/dictionary?code=division_settle_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            showColumn: false,
            width: 110,
          },
          {
            label: "分账规则",
            prop: "divisionRule",
            type: "select",
            search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=division_rule",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            showColumn: false,
            width: 110,
          },
          {
            label: "申请日期",
            prop: "createTime",
            type: "input",
            overHidden: true,
            width: 140,
          },
          {
            label: "申请日期",
            prop: "timeData",
            type: "datetimerange",
            searchRange: true,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "开始时间",
            endPlaceholder: "截至时间",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择申请日期",
                trigger: "blur",
              },
            ],
            hide: true,
            display: false,
          },
        ],
      },
      data: [],
      dialogVisible: false,
      dialogType: "",
      dialogText: "申请开通分账业务",
      merchantForm: {
        rpiNo: "",
        rpiName: "",
        projectCode: "",
        projectName: "",
        divisionKind: "",
        divisionScope: "",
        divisionType: "",
        divisionMode: "",
        settleType: "",
        divisionRule: "",
      },
      rules: {
        projectCode: [
          { required: true, message: "请选择项目", trigger: "change" },
        ],
        rpiNo: [
          { required: true, message: "请选择商户", trigger: "change" },
        ],
        divisionKind: [
          { required: true, message: "请选择分账种类", trigger: "change" },
        ],
        divisionScope: [
          { required: true, message: "请选择分账范围", trigger: "change" },
        ],
        divisionType: [
          { required: true, message: "请选择分账类型", trigger: "change" },
        ],
        divisionMode: [
          { required: true, message: "请选择分账方式", trigger: "change" },
        ],
        settleType: [
          { required: true, message: "请选择结算类型", trigger: "change" },
        ],
        divisionRule: [
          { required: true, message: "请选择分账规则", trigger: "change" },
        ],
      },
      rpiParams: {
        aisleCode: ''
      }
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.separate_merchant_add, false),
        viewBtn: this.vaildData(this.permission.separate_merchant_view, false),
        editBtn: this.vaildData(this.permission.separate_merchant_edit, false),
      };
    },
  },
  watch: {
    "search.timeData"(val) {
      if (val) {
        this.search.createTimeStart = val[0];
        this.search.createTimeEnd = val[1];
      } else {
        this.search.createTimeEnd = "";
        this.search.createTimeStart = "";
      }
    },
  },
  components: {
    selectPage,
    selectDict,
  },
  methods: {
    searchReset() {
      this.query = {};
      this.search = {};
      this.$set(this.search, "timeData", ["", ""]);
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    sortChange(val) {
      if (val.order == "ascending") {
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleSave() {
      this.dialogVisible = true;
      this.dialogType = "add";
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    handleView(row) {
      this.$router.push({
        name: "分账商户详情",
        query: {
          d: this.$crypto.encrypt(
            JSON.stringify({
              id: row.id,
            })
          ),
        },
      });
    },
    projectChange(e, row) {
      if(e){
        this.rpiParams.aisleCode = row.aisleCode
        this.merchantForm.rpiNo = ''
        this.$refs.searchRpiSelect.getOptionList(this.rpiParams)
      }else{
        this.rpiParams.aisleCode = ''
        this.$refs.searchRpiSelect.getOptionList({})
      }
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: "提交中...",
            spinner: "el-icon-loading",
          });
          add(this.merchantForm)
            .then(() => {
              this.$message.success("操作成功");
              this.dialogVisible = false;
              this.refreshChange();
            })
            .finally(() => {
              loading.close();
            });
        }
      });
    },
    handleClose(done) {
      this.$refs.form.resetFields();
      done();
    },
  },
};
</script>

<style lang="scss" scoped>
.separate-merchant {
  /deep/ .el-tag--dark.el-tag--success {
    border: none !important;
  }
}

.haryuki-form-title {
  font-weight: bold;
  text-align: center;
  border-bottom: 1px solid #e4e7ed;
  padding-bottom: 10px;
}

.haryuki-m-b-10 {
  margin-bottom: 10px;
}

::v-deep .disabled-view .el-input__inner {
  background: #f5f7fa !important;
  color: #73757a;
}
</style>